using System.Collections.Generic;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;

namespace ShapeEditor
{
    public partial class SlicePlaneControl : UserControl
    {
        private SlicePlaneProxy slicePlaneProxy;
        private AssetManager assetMan;

        private delegate void SlicePlaneHandler(SlicePlaneProxy slp);

        public SlicePlaneControl()
        {
            InitializeComponent();
            assetMan = AssetManager.Instance;
            assetManagerDropDownControl1.Init(typeof(IShape));
            assetManagerDropDownControl2.Init(typeof(IPlane));

            assetManagerDropDownControl1.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl1_SelectedEvent);
            assetManagerDropDownControl2.SelectedEvent += new IAssetManagedHandler(assetManagerDropDownControl2_SelectedEvent);
        }

        #region Public Properties
        //
        // Public properties
        //
        public SlicePlaneProxy SlicePlane
        {
            get { return slicePlaneProxy; }
        }

        //
        // Set the focus of this control to a new slicePlane.  Be careful of thread issues
        private void SetSlicePlane_thisThread(SlicePlaneProxy SlicePlane)
        {
            slicePlaneProxy = null;
            //
            // Toggle Enabled based on whether the SlicePlane is null or not
            //
            if (SlicePlane == null)
            {
                this.Enabled = false;
            }
            else
            {
                this.Enabled = true;

                //
                // Populate control with SlicePlane values
                //
                IShape ishape = SlicePlane.IShape;
                assetManagerDropDownControl1.SelectItem((IShape)ishape);

                Plane plane = SlicePlane.Plane;
                assetManagerDropDownControl2.SelectItem(plane);
            }
            slicePlaneProxy = SlicePlane;
        }
        //
        // Force the event to get called on the proper thread.  The DataGridView complains if you try
        //  to use it on the wrong thread.
        //
        public void SetSlicePlane(SlicePlaneProxy SlicePlane)
        {
            SlicePlaneHandler newEvent = new SlicePlaneHandler(SetSlicePlane_thisThread);
            object[] args = new object[1];
            args[0] = SlicePlane;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(newEvent, args);
                else
                    newEvent(SlicePlane);
            }
            catch { }
        }

        #endregion

        private void assetManagerDropDownControl1_SelectedEvent(IAssetManaged obj)
        {
            if (slicePlaneProxy != null)
            {
                slicePlaneProxy.IShape = (IShape)obj;
            }
        }

        private void assetManagerDropDownControl2_SelectedEvent(IAssetManaged obj)
        {
            if (slicePlaneProxy != null)
            {
                slicePlaneProxy.Plane = (Plane)obj;
            }
        }


        public SharedBox SelectBoxDefault()
        {
            List<SharedBox> iBox = new List<SharedBox>();
            // Hack for now only SharedBox
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(SharedBox));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is SharedBox)
                    iBox.Add((SharedBox)obj);
            }
            if (iBox.Count > 0)
            {
                assetManagerDropDownControl1.SelectItem((IAssetManaged)iBox[0]);
                return iBox[0];
            }

            return null;
        }

        public Plane SelectPlaneDefault()
        {
            List<Plane> planes = new List<Plane>();
            List<IAssetManaged> list2 = assetMan.FindAll(typeof(Plane));
            foreach (IAssetManaged obj in list2)
            {
                if (obj is Plane)
                    planes.Add((Plane)obj);
            }
            if (planes.Count > 0)
            {
                assetManagerDropDownControl2.SelectItem((IAssetManaged)planes[0]);
                return planes[0];
            }

            return null;
        }
        public delegate void SlicePlaneVisibility(bool isVisible);

        public void Visibility(bool isVisible)
        {
            SlicePlaneVisibility slicePlaneVisibility = new SlicePlaneVisibility(IsVisible_ThisThread);

            object[] args = new object[1];
            args[0] = isVisible;
            //
            // We need to make sure these methods are executed on the correct thread.
            //
            try
            {
                if (this.InvokeRequired)
                    this.Invoke(slicePlaneVisibility, args);
                else
                    slicePlaneVisibility(isVisible);
            }
            catch { }

        }

        public void IsVisible_ThisThread(bool isVisible)
        {
            this.Visible = isVisible;
        }
    }
}
